package com.leetcode.binary_search;

/**
 * @author Dennis Li
 * @date 2020/10/31 17:31
 */
public class SearchInsert_35 {

    public int searchInsert(int[] nums, int target) {
        int i = 0, j = nums.length;
        while (i < j) {
            int mid = i + (j - i + 1) / 2;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] > target) {
                j = mid - 1;
            } else if (nums[mid] < target) {
                i = mid;
            }
        }
        return i;
    }
}
